﻿using System;
using System.Security.Authentication;

namespace SimpleTaskManager
{
    public class User
    {
        public int Id;
        public string Name;
        public string Email;
        public string Password;

        public User()
        {
            Id = GetHashCode();
        }

        public static User Auntenticate(string Email, string Password)
        {
            foreach (User user in Storage.Users)
            {
                if (user.Email == Email && user.Password == Password)
                {
                    return user;
                }
            }
            
            throw new AuthenticationException();
        }

        public static User Load(int Id)
        {
            foreach (var user in Storage.Users)
            {
                if (user.Id == Id)
                {
                    return user;
                }
            }
            return null;
        }


        public void  Save()
        {
            if (!Storage.Users.Contains(this))
            {
                Storage.Users.Add(this);
            }
        }

        public void  Delete()
        {
            Storage.Users.Remove(this);

        }

        public override bool Equals(object obj)
        {
            return ((User) obj).Id == this.Id;
        }

        public static bool operator ==(User a, User b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(User a, User b)
        {
            return !(a == b);
        }
    }
}
